package tp.pr5.commands;

import java.util.StringTokenizer;
import tp.pr5.Directions;
import tp.pr5.Door;
import tp.pr5.Game;
import tp.pr5.Map;
import tp.pr5.MensajesConst;
import tp.pr5.Player;
import tp.pr5.Room;
import tp.pr5.commands.exceptions.CommandExecutionException;
import tp.pr5.commands.exceptions.WrongCommandFormatException;

public class GoCommand implements Command
{

	private Directions _direccion;
	private Game _game;
	private Player _friki;
	@SuppressWarnings("unused")
	private Room _habitacion;
	private Map _map;


	public GoCommand()
	{
		this._direccion = null;
		this._game=null;
		this._friki=null;

	}
	public void configureContext(Game g, Map m, Player p) {
		this._friki=p;
		this._habitacion=m.getCurrentRoom();
		this._game=g;
		this._map=m;
	}
	@Override public void execute() throws CommandExecutionException
	{
		Door auxiliar;

		auxiliar = this._map.getDoor(this._game.getCurrentRoom(), this._direccion); 

		if (auxiliar==null)
		{
			throw new CommandExecutionException(MensajesConst.wrongGateway + this._direccion.toString() + " ?");
		}
		else
		{
			if (auxiliar.isOpen())
			{
				if ((auxiliar.getOrigen()==this._game.getCurrentRoom())||(auxiliar.getBidi())) {
							
					this._friki.addHealth(-5);
				}
				else {
					throw new CommandExecutionException(MensajesConst.noWay);
				}
				if (auxiliar.getDir().getOpuesto()==this._direccion && auxiliar.getBidi())
				{
					this._map.setCurrentRoom(auxiliar.getOrigen());
					this._game.setCurrentRoom(auxiliar.getOrigen());
				}
				else
				{
					this._map.setCurrentRoom(auxiliar.getDestino());
					this._game.setCurrentRoom(auxiliar.getDestino());
					
				}
				
			}
			else
			{
				throw new CommandExecutionException("There is a door in the " + this._direccion.toString() + ", but it is closed.\n");
			}
		}
		if (this._game.getCurrentRoom().isExit()|| this._map.getCurrentRoom().isExit()){	
			//System.out.println(this._game.getCurrentRoom().getDescription());
			
			this._map.playerExamineRoom();
			this._friki.getStatus();			
			 //this._game.requestQuit();
			 return;
		}
		//System.out.println(this._game.getCurrentRoom().getDescription());
		this._map.playerExamineRoom();
		this._friki.getStatus();
		//TODO nuevo!
		this._friki.requestPlayerUpdate();
		
	}

	@Override public String getHelp()
	{
		return MensajesConst.hGo;
	}

	@Override public Command parse(String cad)
			throws WrongCommandFormatException
	{
		StringTokenizer tkn = new StringTokenizer(cad);

		if (tkn.countTokens() != 2)
			throw new WrongCommandFormatException();
		String verb = tkn.nextToken();
		String where = tkn.nextToken();
		if (!(verb.equalsIgnoreCase("GO") || verb.equalsIgnoreCase("IR")))
			throw new WrongCommandFormatException();
		try
		{

			this._direccion = Directions.valueOf(where.toUpperCase());
		//	this._game = execContext;
		//	this._friki = this._friki;
		} catch (Exception e)
		{
			throw new WrongCommandFormatException();
		}

		return this;
	}
}
